package com.ytkj.digitalConstruction.service.constructionprocess;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ytkj.digitalConstruction.dao.constructionprocess.ConstructionPreparationMapper;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionPreparation;
import com.ytkj.digitalConstruction.entity.user.User;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.WholeFlowInfo;

/**
 * 施工准备（风险) 业务
 * 
 * @author 宁~
 *
 */
@Service
@Transactional
public class ConstructionPreparationService {
	
	@Autowired
	private ConstructionBaseService constructionBaseService;
	
	@Autowired
	private ConstructionPreparationMapper constructionPreparationMapper;
	
	/**
	 * @Description: 填写进场时间和进场路线
	 * @Title: setPreparation 
	 * @param constructionPreparation
	 * @return 
	 * @return WholeFlowInfo
	 * @throws
	 */
	public WholeFlowInfo setPreparation(ConstructionPreparation constructionPreparation, User user){
		constructionBaseService.checkProcess(constructionPreparation.getFlowId(), 2);
		// 生成施工准备
		createConstructionPreparation(constructionPreparation, user);
		
		// 新增
		constructionPreparationMapper.insertSelective(constructionPreparation);
		ConstructionUtilService.isAddSuccess(constructionPreparation.getPreparationId());
		
		// 更新进度，发送对应通知并返回项目及流程信息
		return constructionBaseService.processNextResult(constructionPreparation.getFlowId(),user, 2);
	}
	
	/**
	 * @Description: 生成施工准备信息
	 * @Title: createConstructionPreparation 
	 * @param @param constructionPreparation
	 * @param @param userId
	 * @param @return 
	 * @return ConstructionPreparation
	 * @throws
	 */
	public ConstructionPreparation createConstructionPreparation(ConstructionPreparation constructionPreparation, User user){
		patchPreparation(constructionPreparation, user);
		if(constructionPreparation.getPreparationId() != null){
			constructionPreparation.setFlowId(null);
		}
		constructionPreparation.setStatus(1);
		
		return constructionPreparation;
	}

	/**
	 * @Description: 补全参数
	 * @Title: patchPreparation 
	 * @param @param constructionPreparation
	 * @param @param user 
	 * @return void
	 * @throws
	 */
	public void patchPreparation(ConstructionPreparation constructionPreparation, User user) {
		constructionPreparation.setOperator(String.valueOf(user.getUserId())); //节点经办人姓名
		constructionPreparation.setUpdateBy(user.getUserId());
		constructionPreparation.setUpdateDate(new Date());
	}
	
	/**
	 * @Description: 修改进场时间和路线
	 * @Title: updatePreparation 
	 * @param @param constructionPreparation
	 * @param @return 
	 * @return WholeFlowInfo
	 * @throws
	 */
	public WholeFlowInfo updatePreparation(ConstructionPreparation constructionPreparation, User user) {
		// 进入施工区的申请是否已经审核完成
		boolean isConfirmApproach = constructionBaseService.isConfirmApproach(constructionPreparation.getFlowId());
		// 填写施工风险进度是否完成
		boolean isPreparationSetted = constructionBaseService.isProcessOver(constructionPreparation.getFlowId(), 2);
		// 是否已经结束或者取消
		boolean isEndOrCancel = constructionBaseService.isProcessOver(constructionPreparation.getFlowId(), null);
		
		if(isConfirmApproach || !isPreparationSetted || isEndOrCancel) {
			return null;
		}else {
			patchPreparation(constructionPreparation, user);
			constructionPreparationMapper.updateByPrimaryKeySelective(constructionPreparation);
		}
		return constructionBaseService.showWholeFlowByFlowId(constructionPreparation.getFlowId());
	}
	
	/**
	 * @Description: 添加或者修改施工风险
	 * @Title: setOrUpdatePreparation 
	 * @param @param constructionPreparation
	 * @param @return 
	 * @return WholeFlowInfo
	 * @throws
	 */
	public WholeFlowInfo setOrUpdatePreparation(ConstructionPreparation constructionPreparation, User user) {
		return constructionPreparation.getPreparationId()==null?setPreparation(constructionPreparation, user)
				:updatePreparation(constructionPreparation, user);
	}
	
	
	
}
